<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/24.
 */

namespace App\Models;

use App\Models\Traits\ModelField;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticate;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * @property int id
 * @property string username
 * @property string nickname
 * @property string password
 * @property string mobile
 * @property string avatar
 * @property string login_at
 * @property string login_ip
 * @property int is_founder
 * @property int status
 */
class System extends Authenticate implements JWTSubject
{
    use HasRoles;
    use ModelField;
    use SoftDeletes;

    protected $hidden = ['password'];
    protected $guard_name;

    public function __construct()
    {
        $this->guard_name = config('auth.app_guard_name');
        parent::__construct();
    }

    public function getFillable(): array
    {
        if ($this->fillable) {
            return $this->fillable;
        }

        return $this->getTableFields();
    }

    /**
     * 获取当前时间.
     */
    public function freshTimestamp(): int
    {
        return time();
    }

    /**
     * 避免转换时间戳为时间字符串.
     *
     * @param int $value
     *
     * @return int
     */
    public function fromDateTime($value): int
    {
        return $value;
    }

    /**
     * 从数据库获取的为获取时间戳格式.
     */
    public function getDateFormat(): string
    {
        return 'U';
    }

    public function getCreatedAtAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['created_at']);
    }

    public function getUpdatedAtAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['updated_at']);
    }

    public function getLoginAtAttribute()
    {
        if ($this->attributes['login_at']) {
            return date('Y-m-d H:i:s', $this->attributes['login_at']);
        }

        return '';
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * 自定义jwt声明.
     *
     * @return array
     */
    public function getJWTCustomClaims(): array
    {
        return [];
    }
}
